Skip to content

fix(session): distinguish rejected record values from internal errors - #491

Closed
franks3yk wants to merge 1 commit into
aimdb-dev:mainfrom
franks3yk:fix/454-invalid-record-value-error
Closed

franks3yk wants to merge 1 commit into
aimdb-dev:mainfrom
franks3yk:fix/454-invalid-record-value-error

Conversation

@franks3yk

Copy link
Copy Markdown

Description

Problem

When a remote record.set value is well-formed JSON but does not match the record's Rust type or fails validation, the write is correctly refused and the session remains healthy. The client nevertheless reports:

Server error (code internal): engine/transport failure

That points at the engine or transport instead of the caller's value, which makes CLI and MCP clients retry or diagnose the wrong layer.

Root cause

The rejection lost its identity at four points:

  1. TypedRecord::set_from_json / set_from_json_bytes collapsed every codec failure into DbError::RuntimeError, discarding the deserializer's explanation.
  2. AimX's map_db_err routed every error except not-found and permission failures to RpcError::Internal.
  3. RpcError had no variant for an invalid caller value, and the AimX error frame carried only err, even though the envelope already reserves an optional msg.
  4. aimdb-client mapped the resulting internal error to its generic engine/transport message.

Fix

  • Add DbError::InvalidRecordValue { type_name, reason }, classified as DbErrorKind::Data.
  • Add detail-preserving decode methods to RemoteSerialize and JsonCodec with default implementations, so existing manual/custom codecs remain source-compatible. The Serde-backed implementations retain the original serde_json error text.
  • Add RpcError::InvalidValue(String) and map it to the AimX wire code invalid_value with the reserved optional msg field.
  • Decode msg as Cow<str> so messages containing escaped quotes or other escapes do not fail frame borrowing/decoding.
  • Map the new code through aimdb-client and the WASM bridge. CLI and MCP already render ClientError::ServerError, so they now expose the specific code and deserializer message without separate changes.
  • Document the error envelope in aimdb-client/README.md.

The change is additive on the wire. An older client that does not know invalid_value continues to decode it as internal, and a missing msg falls back to a generic invalid-value explanation. Unknown future error codes retain the existing internal fallback.

Regression coverage

  • Codec tests verify that Serde rejection reasons survive both tree and byte decoding, while a custom codec using the new default methods still works.
  • Typed-record tests verify that both remote write entrypoints return InvalidRecordValue with the type name and reason.
  • AimX codec tests cover wire encoding/decoding, escaped quotes in msg, a missing msg, and unknown-code backward compatibility.
  • A dispatch test drives a real record.set through AimxDispatch and verifies that a rejected value returns InvalidValue, followed by a successful write on the same session.
  • A UDS end-to-end test connects with AimxConnection, writes a valid value, verifies that an invalid value returns invalid_value plus the serde reason, verifies that the stored value is unchanged, and then writes a corrected value on the same connection.

Validation

All commands were run with Rust 1.98.0 from rust-toolchain.toml.

  • make test passed on the final commit with exit code 0. This is the repository's full workspace test target, including operational-event/build-info link checks and the std/no_std adapter and connector suites. The captured log contains 464 test result: ok lines and no failed test result.
  • cargo test --quiet -p aimdb-core --features 'std,remote'
    • 303 lib tests passed
    • 1 log_targets test passed
    • 20 session-engine tests passed
    • 17 doc tests passed, 16 ignored
  • cargo test --quiet -p aimdb-client
    • 32 lib tests passed
    • 8 aimx_session integration tests passed
    • 5 instance_info tests passed
    • 2 pump_client tests passed
    • 2 doc tests passed, 1 ignored
  • cargo test --quiet -p aimdb-core --no-default-features --features 'alloc,remote'
    • 166 lib tests passed
    • 17 doc tests passed, 16 ignored
  • cargo test --quiet -p aimdb-wasm-adapter --no-default-features --lib
    • 14 tests passed
  • make check-toolchain-pin and make fmt-check passed.
  • make clippy passed with -D warnings across every valid workspace feature combination, including core, client, CLI, MCP, WebSocket, UDS, serial, TCP, MQTT, KNX, Zenoh, Embassy, the WASM target, and benchmarks.
  • cargo check -p aimdb-wasm-adapter --target wasm32-unknown-unknown --features wasm-runtime passed.
  • cargo check -p aimdb-cli -p aimdb-mcp -p aimdb-websocket-connector -p aimdb-uds-connector passed.
  • make test-embedded passed, including the full no_std AimX server combinations on thumbv7em-none-eabihf.
  • make check-no-sim passed, including the positive control that the sim build does pull in rand.

cargo deny check was also run. It fails on the pre-existing RUSTSEC-2026-0543 advisory for chrono 0.4.42 (unmaintained, no safe upgrade available), which reaches the workspace through existing aimdb-cli, aimdb-mcp, and embassy-stm32 dependencies. The summary is advisories FAILED, bans ok, licenses ok, sources ok. This branch does not change Cargo.toml, Cargo.lock, or deny.toml, so the PR intentionally avoids mixing an unrelated dependency migration or advisory suppression into this fix.

The aggregate make check target was therefore not green as a single command because its deny leg hits that upstream-existing advisory. Its other legs, including the WASM target check, were run directly as listed above. The separate browser test target make wasm-test was not run because wasm-pack, Chrome/Chromium, and chromedriver are unavailable in this environment.

Related Issue

Fixes #454

Checklist

  • I have read the CONTRIBUTING.md document.
  • My code follows the project's coding standards.
  • I have added tests to cover my changes.
  • All new and existing tests passed (make check).
    • The affected suites and cross-target checks listed under Validation pass. The aggregate target is blocked only by the pre-existing chrono advisory described above; the separate browser WASM tests were not run because browser tooling is absent.
  • I have updated the documentation accordingly.

@franks3yk
franks3yk requested a review from lxsaah as a code owner November 18, 2026 01:58
Comment thread aimdb-core/src/codec.rs
return Ok(value);
}
let value = serde_json::from_slice(bytes).map_err(|e| e.to_string())?;
self.decode_with_error(&value)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preserve the byte decoder’s rejection. A custom decode_slice can reject duplicate object keys. This fallback parses the same bytes into serde_json::Value, which collapses the duplicates. If the tree decoder accepts the resulting object, set_from_json_bytes commits a write the codec previously rejected.

Please map self.decode_slice(bytes) returning None directly to a generic error and apply the same change to RemoteSerialize::from_json_slice_with_error. The Serde overrides already preserve detailed errors. A regression test where the byte decoder rejects duplicate keys and the tree decoder accepts the collapsed object would cover this case.

@franks3yk franks3yk closed this by deleting the head repository Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] record.set reports a rejected value as internal: engine/transport failure

2 participants